home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1757 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  55 lines

  1. Newsgroups: comp.lang.c
  2. Path: mvb.saic.com!eskimo!news
  3. From: mag@eskimo.com (mAg)
  4. Subject: Re: Checking for a file => Does it exist (Help)
  5. X-Nntp-Posting-Host: tia1.eskimo.com
  6. Message-ID: <DL9GIA.4y1@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: *.*
  9. References: <4d9k6fINNnja@faatcrl.faa.gov> <4dc6kq$dja@gryphon.phoenix.net>
  10. Date: Tue, 16 Jan 1996 06:25:21 GMT
  11.  
  12. In article <4dc6kq$dja@gryphon.phoenix.net> (Mon, 15 Jan 1996 01:00:31 GMT), 
  13. brucew@phoenix.net says :
  14. >
  15. >afrawert@faatcrl.faa.gov (Alan Frawert) wrote:
  16. >
  17. >
  18. >>I'm having a problem just checking to see if a file
  19. >>exists. Does anyone have a simple piece of code that I can use?
  20. >
  21. >Well, you could try to open it then close it:
  22. >
  23. >int file_exist( char *fname)
  24. >{
  25. >    int flag = 0;
  26. >    FILE *fp = fopen( fname, "r");
  27. >    flag = fp ?  1 : 0;
  28.     >    fclose(fp);
  29. >    return flag;
  30. >}
  31.  
  32. Just a little smarter(?) version :-)
  33.  
  34. int file_exist(char *fname)
  35. {
  36. FILE *fp;
  37. if (fp = fopen(fname,"r"))
  38.   {
  39.   fclose(fp);
  40.   return(1);
  41.   }
  42. return (0);
  43. }
  44.  
  45. -- 
  46. /* --------------------------------------------------------
  47.                       MAG@ESKIMO.COM
  48. http://www.eskimo.com/~mag/index.html
  49. ***********************************************************
  50. To understand recursion one must first understand recursion
  51. ***********************************************************
  52. -------------------------------------------------------- */
  53.  
  54.